package edu.ccut.saturn.studio.naviagtor.wizard.model;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnValidation;
import edu.ccut.saturn.studio.core.util.wizard.SaturnWizardPage;

public class CreateModelWizardPage extends SaturnWizardPage {

	public static final String SATURN_FLODER_MODEL_DESCRIPTION = "description"; //$NON-NLS-1$
	public static final String SATURN_FLODER_MODEL_NAME = "name"; //$NON-NLS-1$
	public static final String SATURN_FLODER_MODEL_FILE_NAME = "floder"; //$NON-NLS-1$
	public static final String SATURN_FLODER_MODEL_VERSION = "version"; //$NON-NLS-1$
	public static final String SATURN_FLODER_MODEL_TIME = "time"; //$NON-NLS-1$
	public static final String SATURN_FLODER_MODEL_TYPE = "type"; //$NON-NLS-1$
	public static final String SATURN_FLODER_MODEL_SELECT = "select"; //$NON-NLS-1$

	private Text descriptionText;
	private Text folderNameText;
	private Text nameText;
	private Text versionTxt;

	private Button actionButton;
	private Button componentButton;
	private Button dataButton;
	private Button logicButton;
	private Button pageButton;
	private Button validateButton;
	private Button workflowButton;

	private static int count = 1;

	private List<String> selectList = new ArrayList<String>();

	/**
	 * Create the wizard
	 */
	public CreateModelWizardPage() {
		super("wizardPage"); //$NON-NLS-1$
		setTitle(Messages.getString("CreateModelWizardPage.module")); //$NON-NLS-1$
		setDescription(Messages
				.getString("CreateModelWizardPage.createonemodule")); //$NON-NLS-1$

	}

	@Override
	public void initalValues(Map<String, Object> values) {
		values.put(SATURN_FLODER_MODEL_FILE_NAME, folderNameText.getText()
				.trim());
		values.put(SATURN_FLODER_MODEL_NAME, nameText.getText().trim());
		values.put(SATURN_FLODER_MODEL_DESCRIPTION, descriptionText.getText()
				.trim());
		values.put(SATURN_FLODER_MODEL_VERSION, versionTxt.getText().trim());
		values.put(SATURN_FLODER_MODEL_TIME, System.currentTimeMillis() + ""); //$NON-NLS-1$
		values.put(SATURN_FLODER_MODEL_TYPE, "module"); //$NON-NLS-1$

		values.put(SATURN_FLODER_MODEL_SELECT, this.getSelectList());
	}

	/**
	 * Create contents of the wizard
	 * 
	 * @param parent
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		container.setLayout(gridLayout);
		initializeDialogUnits(parent);
		setControl(container);

		final Label label = new Label(container, SWT.NONE);
		label.setText(Messages.getString("CreateModelWizardPage.modulename")); //$NON-NLS-1$

		folderNameText = new Text(container, SWT.BORDER);
		folderNameText.setTextLimit(50);
		final GridData gd_folderName = new GridData(SWT.FILL, SWT.CENTER, true,
				false);
		folderNameText.setLayoutData(gd_folderName);
		folderNameText.setText("Module"); //$NON-NLS-1$
		folderNameText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validate();
			}
		});

		final Label label2 = new Label(container, SWT.NONE);
		label2.setText(Messages.getString("CreateModelWizardPage.name")); //$NON-NLS-1$

		nameText = new Text(container, SWT.BORDER);
		nameText.setTextLimit(50);
		nameText.setText(Messages.getString("CreateModelWizardPage.0")); //$NON-NLS-1$
		final GridData gd_folderName1 = new GridData(SWT.FILL, SWT.CENTER,
				true, false);
		nameText.setLayoutData(gd_folderName1);
		nameText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validate();
			}
		});

		final Label label_2 = new Label(container, SWT.NONE);
		label_2.setText(Messages.getString("CreateModelWizardPage.version")); //$NON-NLS-1$

		versionTxt = new Text(container, SWT.BORDER);
		versionTxt.setTextLimit(50);
		versionTxt.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validate();
			}
		});
		final GridData gd_versionTxt = new GridData(SWT.FILL, SWT.CENTER, true,
				false);
		versionTxt.setLayoutData(gd_versionTxt);

		final Label label_1 = new Label(container, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false));
		label_1
				.setText(Messages
						.getString("CreateModelWizardPage.description")); //$NON-NLS-1$

		descriptionText = new Text(container, SWT.V_SCROLL | SWT.BORDER
				| SWT.H_SCROLL);
		descriptionText.setTextLimit(200);
		final GridData gd_decriptionText = new GridData(SWT.FILL, SWT.FILL,
				true, true);
		gd_decriptionText.heightHint = 49;
		descriptionText.setLayoutData(gd_decriptionText);

		final Group group = new Group(container, SWT.NONE);
		group.setText(Messages
				.getString("CreateModelWizardPage.createcomponentfile")); //$NON-NLS-1$
		group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
		final GridLayout gridLayout_1 = new GridLayout();
		gridLayout_1.numColumns = 3;
		group.setLayout(gridLayout_1);

		actionButton = new Button(group, SWT.CHECK);
		actionButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false));
		actionButton.setText(Messages
				.getString("CreateModelWizardPage.emergecomponentfile")); //$NON-NLS-1$

		componentButton = new Button(group, SWT.CHECK);
		componentButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false));
		componentButton.setText(Messages
				.getString("CreateModelWizardPage.basiccomponentfile")); //$NON-NLS-1$

		dataButton = new Button(group, SWT.CHECK);
		dataButton
				.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		dataButton.setText(Messages
				.getString("CreateModelWizardPage.dataitemfile")); //$NON-NLS-1$

		logicButton = new Button(group, SWT.CHECK);
		final GridData gd_logicButton = new GridData();
		logicButton.setLayoutData(gd_logicButton);
		logicButton.setText(Messages
				.getString("CreateModelWizardPage.logiccomponentfile")); //$NON-NLS-1$

		pageButton = new Button(group, SWT.CHECK);
		pageButton.setText(Messages
				.getString("CreateModelWizardPage.pagecomponentfile")); //$NON-NLS-1$

		// scaButton = new Button(group, SWT.CHECK);
		// scaButton.setText(Messages.getString("CreateModelWizardPage.13"));
		// //$NON-NLS-1$

		validateButton = new Button(group, SWT.CHECK);
		validateButton.setText(Messages
				.getString("CreateModelWizardPage.validatecomponentfile")); //$NON-NLS-1$

		workflowButton = new Button(group, SWT.CHECK);
		workflowButton.setText(Messages
				.getString("CreateModelWizardPage.workflowfile")); //$NON-NLS-1$

		final Composite buttonComposite = new Composite(container, SWT.NONE);
		buttonComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				true, 2, 1));
		final GridLayout gridLayout_2 = new GridLayout();
		gridLayout_2.numColumns = 2;
		buttonComposite.setLayout(gridLayout_2);

		final Button allButton = new Button(buttonComposite, SWT.NONE);
		allButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				actionButton.setSelection(true);
				componentButton.setSelection(true);
				dataButton.setSelection(true);
				logicButton.setSelection(true);
				pageButton.setSelection(true);
				// scaButton.setSelection(true);
				validateButton.setSelection(true);
				workflowButton.setSelection(true);
			}
		});
		allButton.setLayoutData(new GridData(60, SWT.DEFAULT));
		allButton.setText(Messages.getString("CreateModelWizardPage.checkall")); //$NON-NLS-1$

		final Button reverseButton = new Button(buttonComposite, SWT.NONE);
		reverseButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				actionButton.setSelection(!actionButton.getSelection());
				componentButton.setSelection(!componentButton.getSelection());
				dataButton.setSelection(!dataButton.getSelection());
				logicButton.setSelection(!logicButton.getSelection());
				pageButton.setSelection(!pageButton.getSelection());
				// scaButton.setSelection(!scaButton.getSelection());
				validateButton.setSelection(!validateButton.getSelection());
				workflowButton.setSelection(!workflowButton.getSelection());
			}
		});
		reverseButton.setLayoutData(new GridData(60, SWT.DEFAULT));
		reverseButton.setText(Messages
				.getString("CreateModelWizardPage.anticheck")); //$NON-NLS-1$

		validate();
	}

	public List<String> getSelectList() {
		if (actionButton.getSelection()) {
			selectList.add(SaturnModelTypes.ACTION); //$NON-NLS-1$
		}
		if (componentButton.getSelection()) {
			selectList.add(SaturnModelTypes.COMPONENT); //$NON-NLS-1$
		}
		if (dataButton.getSelection()) {
			selectList.add(SaturnModelTypes.DATA); //$NON-NLS-1$
		}
		if (logicButton.getSelection()) {
			selectList.add(SaturnModelTypes.LOGIC); //$NON-NLS-1$
		}
		if (pageButton.getSelection()) {
			selectList.add(SaturnModelTypes.WEB); //$NON-NLS-1$
		}
		if (validateButton.getSelection()) {
			selectList.add(SaturnModelTypes.VALIDATE); //$NON-NLS-1$
		}
		if (workflowButton.getSelection()) {
			selectList.add(SaturnModelTypes.WORKFLOW); //$NON-NLS-1$
		}

		return selectList;
	}

	public boolean validate() {
		String folder = this.folderNameText.getText().trim();
		String name = this.nameText.getText().trim();
		String version = this.versionTxt.getText().trim();

		File file = new File(super.getFullPath() + File.separator + folder);
		setPageComplete(false);

		if (SaturnValidation.isEmpty(folder)) {
			setErrorMessage(Messages
					.getString("CreateModelWizardPage.modulenamecannotempty")); //$NON-NLS-1$
		} else if (SaturnValidation.isPriNum(folder)) {
			setErrorMessage(Messages
					.getString("CreateModelWizardPage.moudleFirstNotContaiNumber")); //$NON-NLS-1$
		} else if (!SaturnValidation.isValidProgramStr(folder)) {
			setErrorMessage(Messages.getString("CreateModelWizardPage.floderNumberLetterUnderline")); //$NON-NLS-1$
		} else if (!folder.endsWith(Messages.getString("CreateModelWizardPage.1")) && !folder.endsWith(Messages.getString("CreateModelWizardPage.2"))) { //$NON-NLS-1$ //$NON-NLS-2$
			setErrorMessage(Messages.getString("CreateModelWizardPage.floderEndwith")); //$NON-NLS-1$
		} else if (file.exists()) {
			setErrorMessage(Messages
					.getString("CreateModelWizardPage.moduleexist")); //$NON-NLS-1$
		}

		else if (SaturnValidation.isEmpty(name)) {
			setErrorMessage(Messages
					.getString("CreateModelWizardPage.NameIsEmpty")); //$NON-NLS-1$
		} else if (SaturnValidation.isBadWord(name)) {
			setErrorMessage(Messages
					.getString("CreateModelWizardPage.NameIsBadword")); //$NON-NLS-1$
		} else if (SaturnValidation.isEmpty(version)) {
			setErrorMessage(Messages
					.getString("CreateModelWizardPage.VersionIsEmpty")); //$NON-NLS-1$
		} else if (SaturnValidation.isBadWord(version)) {
			setErrorMessage(Messages
					.getString("CreateModelWizardPage.VersionIsBadword")); //$NON-NLS-1$
		} else {
			setErrorMessage(null);
			setPageComplete(true);
			return true;
		}

		return false;
	}
}